package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jmp.base.dto.PayWxCashRequest;
import com.jmp.feign.PayFeign;
import one.stand.mapper.RoyaltyRecordMapper;
import one.stand.model.ResultModel;
import one.stand.model.RoyaltyRecordModel;
import one.stand.request.RoyaltyRecordRequest;
import one.stand.response.RoyaltyRecordResponse;
import one.stand.service.UserService;
import one.stand.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private PayFeign payFeign;
    @Autowired
    protected RoyaltyRecordMapper royaltyRecordMapper;

    @Override
    public ResultModel<Map<String, Object>> selectRoyaltyRecord(RoyaltyRecordRequest request) {
        if (request == null || request.getType() == null) {
            return ResultModel.checkParamFail();
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<RoyaltyRecordModel> list = royaltyRecordMapper.select(new RoyaltyRecordModel() {{
            setType(request.getType());
            setUserId(request.getId().intValue());
        }});
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        List<RoyaltyRecordResponse> royaltyRecordResponses = new ArrayList<>();
        list.forEach(obj -> {
            {
                RoyaltyRecordResponse royaltyRecordResponse = new RoyaltyRecordResponse();
                BeanUtils.copyProperties(obj, royaltyRecordResponse);
                royaltyRecordResponses.add(royaltyRecordResponse);
            }
        });
        PageInfo<RoyaltyRecordResponse> pageInfo = new PageInfo<>(royaltyRecordResponses);
        Map<String, Object> map = new HashMap<>(3);
        map.put("list", JSON.toJSONString(pageInfo.getList()));
        map.put("total", pageInfo.getTotal());
        map.put("allMoney", royaltyRecordMapper.selectAllMoney(new RoyaltyRecordModel() {{
            setType(request.getType());
            setUserId(request.getId().intValue());
        }}));
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<Boolean> doCash(RoyaltyRecordRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        try {
            //微信提现申请
            PayWxCashRequest payWxCashRequest = PayWxCashRequest.builder()
                    .recordNo("")
                    .openid("")
                    .amount(request.getRoyaltyMoney())
                    .ip("")
                    .companyKey(request.getCompanyKey())
                    .build();
            ResultModel resultModel = payFeign.wxCash(payWxCashRequest);
            if (resultModel.isSuccess()) {
                // 操作成功
                RoyaltyRecordModel model = new RoyaltyRecordModel();
                model.setUserId(request.getId().intValue());
                model.setRoyaltyMoney(request.getRoyaltyMoney());
                model.setType(2);
                model.setCreateTime(DateUtil.currentDate());
                model.setIsDel(0);
                royaltyRecordMapper.insertSelective(model);
            }
            return resultModel;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultModel.fail();
    }
}
